<?php

include "opendb.php";
// file format:
set_time_limit(0);

if (file_exists('tabular.xml')) {
    $masterlist = array();

    $xml = simplexml_load_file('tabular.xml');

    function process_xml($data, $pid) {
        global $mysqli;
        global $masterlist;

        $diag = new stdClass();
        $diag->pid = $pid;
        if (property_exists($data, "name")) {
            $diag->name = (string) $data->name;
        }
        if (property_exists($data, "desc")) {
            $diag->desc = $mysqli->real_escape_string((string) $data->desc);
        }

        /* Prepared statement to insert data */
        if (!($stmt = $mysqli->prepare("INSERT INTO diag (name, descr, pid) VALUES (?, ?, ?)"))) {
            echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
        }

        $stmt->bind_param('ssi', $diag->name, $diag->desc, $diag->pid);

        /* execute prepared statement */
        $stmt->execute();

        $diag->pid = $mysqli->insert_id;

        /* explicit close recommended */
        $stmt->close();

//        includes_excludes table, ie_maping table
//         id,note,type////// id,ie,diag
        if (property_exists($data, "includes")) {
            $note = new stdClass();
            $note->note = $data->includes->note;
            $note->pid = $diag->pid;
            $note->type = "includes";
            storeNote($note);
        }
        if (property_exists($data, "excludes1")) {
            $note = new stdClass();
            $note->note = $data->excludes1->note;
            $note->pid = $diag->pid;
            $note->type = "includes";
            storeNote($note);
        }
        if (property_exists($data, "excludes2")) {
            $note = new stdClass();
            $note->note = $data->excludes2->note;
            $note->pid = $diag->pid;
            $note->type = "excludes2";
            storeNote($note);
        }
        if (property_exists($data, "useAdditionalCode")) {
            $note = new stdClass();
            $note->note = $data->useAdditionalCode->note;
            $note->pid = $diag->pid;
            $note->type = "useAdditionalCode";
            storeNote($note);
        }
        if (property_exists($data, "inclusionTerm")) {
            $note = new stdClass();
            $note->note = $data->inclusionTerm->note;
            $note->pid = $diag->pid;
            $note->type = "inclusionTerm";
            storeNote($note);
        }
        if (property_exists($data, "codeAlso")) {
            $note = new stdClass();
            $note->note = $data->codeAlso->note;
            $note->pid = $diag->pid;
            $note->type = "codeAlso";
            storeNote($note);
        }
        if (property_exists($data, "codeFirst")) {
            $note = new stdClass();
            $note->note = $data->codeFirst->note;
            $note->pid = $diag->pid;
            $note->type = "codeFirst";
            storeNote($note);
        }
        if (property_exists($data, "notes")) {
            $note = new stdClass();
            $note->note = $data->notes->note;
            $note->pid = $diag->pid;
            $note->type = "notes";
            storeNote($note);
        }

        if (property_exists($data, "section")) {
            foreach ($data->section as $key => $section) {
                $section->name = (string) $section['id'];
                process_xml($section, $diag->pid);
            }
        }

        if (property_exists($data, "diag")) {
            foreach ($data->diag as $key => $_diag) {
                process_xml($_diag, $diag->pid);
            }
        }
    }

    function storeNote($data) {
        global $mysqli;
        /* Prepared statement to insert data */
        if (!($stmt = $mysqli->prepare("INSERT INTO note  (note, type, pid) VALUES (?, ?, ?)"))) {
            echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
        }

        foreach ($data->note as $val) {
            $_note = (string)$val;
            $stmt->bind_param('ssi', $_note, $data->type, $data->pid);

            /* execute prepared statement */
            $stmt->execute();
        }

        /* explicit close recommended */
        $stmt->close();
    }

    foreach($xml->chapter as $chapter){
         process_xml($chapter, 0);
    }

} else {
    exit('Failed to open test.xml.');
}

function print_object($obj) {
    echo "<pre>" . print_r($obj, true) . "</pre>";
}

?>